package com.example.assisteddriving.service;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.Nullable;


import com.example.assisteddriving.R;
import com.example.assisteddriving.Zhuye;
import com.example.assisteddriving.bean.Onedrivelog;
import com.example.assisteddriving.dao.DrivelogDao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Callservice extends Service {
    ServerSocket ss;
    Socket socket;
    Thread callthread,jishithread;
    Runnable calltask;
    private Message msg;
    private PowerManager.WakeLock wakeLock = null;
    public  String PREFERENCE_NAME = "log";  //文件名
    public int MODE = MODE_PRIVATE;  //操作模式
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        calltask=new Runnable() {
            @Override
            public void run() {
                try {
                    ss = new ServerSocket(10008);
                    Onedrivelog onedrivelog;
                    SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    while(true) {
                        Log.v("asd","语音提示等待连接");
                        socket = ss.accept();
                        Log.v("asd","语音提示已连接");

                        BufferedReader br = null;
                        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String line = br.readLine();
                        if (line.equals("start")){
                            Log.v("asd","start");
                            if (sharedPreferences.getInt("log",0)==0){
                                editor.putInt("log",1);
                                editor.commit();
                                java.util.Date gettime=new java.util.Date();
                                onedrivelog=new Onedrivelog();
                                //插入数据库
                                onedrivelog.setLog(String.valueOf(sharedPreferences.getInt("log",0)));
                                onedrivelog.setTired("0");
                                onedrivelog.setSleep("0");
                                onedrivelog.setTime(gettime.getTime());
                                DrivelogDao drivelogDao=new DrivelogDao(Callservice.this);
                                drivelogDao.insert(onedrivelog);
                            } else{
                            editor.putInt("log",sharedPreferences.getInt("log",0)+1);
                            editor.commit();
                                java.util.Date gettime=new java.util.Date();
                                onedrivelog=new Onedrivelog();
                                //插入数据库
                                onedrivelog.setLog(String.valueOf(sharedPreferences.getInt("log",0)));
                                onedrivelog.setTired("0");
                                onedrivelog.setSleep("0");
                                onedrivelog.setTime(gettime.getTime());
                                DrivelogDao drivelogDao=new DrivelogDao(Callservice.this);
                                drivelogDao.insert(onedrivelog);
                            }
                        }
                        if (line.equals("end")){
                            Log.v("asd","end");
                        }
                        if (line.equals("1")) {
                            onedrivelog=new Onedrivelog();
                            java.util.Date gettime=new java.util.Date();
                            Log.v("asd", "sleep");
                            //插入数据库
                            onedrivelog.setLog(String.valueOf(sharedPreferences.getInt("log",0)));
                            onedrivelog.setTired("0");
                            onedrivelog.setSleep("1");
                            onedrivelog.setTime(gettime.getTime());
                            DrivelogDao drivelogDao=new DrivelogDao(Callservice.this);
                            drivelogDao.insert(onedrivelog);

                            MediaPlayer mediaPlayer;
                            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.sleep);
                            if (!mediaPlayer.isPlaying()) mediaPlayer.start();
                            else if (mediaPlayer.isPlaying()) {
                                mediaPlayer.stop();
                                mediaPlayer.start();
                            }
                        }
                        else if(line.equals("2")){
                            Log.v("asd", "tired");
                            onedrivelog=new Onedrivelog();
                            java.util.Date gettime=new java.util.Date();
                            //插入数据库
                            onedrivelog.setLog(String.valueOf(sharedPreferences.getInt("log",0)));
                            onedrivelog.setTired("1");
                            onedrivelog.setSleep("0");
                            onedrivelog.setTime(gettime.getTime());
                            DrivelogDao drivelogDao=new DrivelogDao(Callservice.this);
                            drivelogDao.insert(onedrivelog);

                            MediaPlayer mediaPlayer;
                            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tired);
                            if (!mediaPlayer.isPlaying()) mediaPlayer.start();
                            else if (mediaPlayer.isPlaying()) {
                                mediaPlayer.stop();
                                mediaPlayer.start();
                            }

                        }
                        else if (line.equals("N")){
                            Log.v("asd","xiyan");
                            MediaPlayer mediaPlayer;
                            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.xiyan);

                            if (!mediaPlayer.isPlaying()) mediaPlayer.start();
                            else if (mediaPlayer.isPlaying()) {
                                mediaPlayer.stop();
                                mediaPlayer.start();
                            }

                        }
                        br.close();
                        socket.close();


                    }
                }catch (IOException e){
                    e.printStackTrace();
                }
                finally {
                    try {
                        //关闭输入流、socket
                        if(socket!=null)    socket.close();
                        if(ss!=null) ss.close();
                    } catch (IOException e) {    e.printStackTrace(); }
                }

            }
        };
        callthread=new Thread(calltask);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(!callthread.isAlive()){
            callthread.start();
        }

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(callthread.isAlive()){
            Log.v("asd","语音服务已结束");
            try {
                if(socket!=null)    socket.close();
                if(ss!=null) ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        msg = new Message(); //也可msg = Message.obtain();
        msg.what = 0x001; //设置消息类型
        Bundle data1 = new Bundle(); //生成Bundle对象携带数据
        data1.putString("msg","destroy");
        msg.setData(data1);
        Zhuye.myHandler.sendMessage(msg);

    }
}
